#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 35;

int n, k, m;
int a[N][N], b[N][N], c[N][N];
void matmul(int a[N][N], int b[N][N], int c[N][N]) {
  int t[N][N] = {0};
  rep(i, 1, n) rep(j, 1, n) rep(k, 1, n) {
    t[i][j] = (t[i][j] + a[i][k] * b[k][j] % m) % m;
  }
  rep(i, 1, n) rep(j, 1, n) c[i][j] = t[i][j];
}
void matpow(int b[N][N], int p, int c[N][N]) {
  int bb[N][N];
  int t[N][N] = {0};
  rep(i, 1, n) t[i][i] = 1;
  rep(i, 1, n) rep(j, 1, n) bb[i][j] = b[i][j];
  while (p) {
    if (p & 1) matmul(t, bb, t);
    matmul(bb, bb, bb);
    p >>= 1;
  }
  rep(i, 1, n) rep(j, 1, n) c[i][j] = t[i][j];
}
void matadd(int a[N][N], int b[N][N], int c[N][N]) {
  int t[N][N] = {0};
  rep(i, 1, n) rep(j, 1, n) t[i][j] = (a[i][j] + b[i][j]) % m;
  rep(i, 1, n) rep(j, 1, n) c[i][j] = t[i][j];
}
void f(int a[N][N], int k, int b[N][N]) {
  if (k == 0) {
    rep(i, 1, n) rep(j, 1, n) b[i][j] = 0;
    return;
  }
  int sum[N][N] = {0};
  int t[N][N];
  int I[N][N] = {0};
  rep(i, 1, n) I[i][i] = 1;
  f(a, k / 2, sum);
  matpow(a, k / 2, t);
  matadd(I, t, t);
  matmul(sum, t, sum);
  if (k & 1) {
    matpow(a, k, t);
    matadd(sum, t, sum);
  }
  rep(i, 1, n) rep(j, 1, n) b[i][j] = sum[i][j];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> k >> m;
  rep(i, 1, n) rep(j, 1, n) cin >> a[i][j];
  f(a, k, b);
  rep(i, 1, n) rep(j, 1, n) cout << b[i][j] << " \n"[j == n];
  return 0;
}